﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="QueryOrder.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：QueryOrder.cs
// 创建时间：2014-12-25 12:43
// 修改时间：2014-12-24 14:27
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace Payment.WapYinlian
{
    #region

    using System;

    using Microsoft.Win32.SafeHandles;

    using Newtonsoft.Json;

    using WebCommon;
 

    #endregion

    /// <summary>
    ///     查询订单模型
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class WapYlQuery
    {
        #region Fields

        /// <summary>
        /// The wap yl config.
        /// </summary>
        private WapYlConfig wapYlConfig;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 银联查询接口
        /// </summary>
        /// <param name="wapYlConfig">
        /// The wap yl config.
        /// </param>
        /// <param name="orderid">
        /// The 商户订单号.
        /// </param>
        /// <param name="transid">
        /// The 交易号.
        /// </param>

        public WapYlQuery(WapYlConfig wapYlConfig, string orderid, string transid)
        {
            this.wapYlConfig = wapYlConfig;
            this.MerOrderId = orderid;
            this.TransId = transid;
            this.TransCode = "201203";
            this.ReqTime = DateTime.Now.ToString("yyyyMMddhhmmss");
            this.OrderDate = DateTime.Now.ToString("yyyyMMdd");
            this.MerId = wapYlConfig.MerId;
            this.MerTermId = wapYlConfig.MerTermId;
            this.Url = "https://116.228.21.162:8603/merFrontMgr/orderQueryServlet";
            this.Reserve = string.Empty;
        }

        #endregion

        #region Public Properties

        public string Url { get; set; }

        /// <summary>
        /// Gets or sets the mer id.
        /// </summary>
        [JsonProperty("MerId")]
        public string MerId { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        [JsonProperty("MerOrderId")]
        public string MerOrderId { get; set; }

        /// <summary>
        /// Gets or sets the mer sign.
        /// </summary>
        [JsonProperty("MerSign")]
        public string MerSign { get; set; }

        /// <summary>
        /// Gets or sets the mer term id.
        /// </summary>
        [JsonProperty("MerTermId")]
        public string MerTermId { get; set; }

        /// <summary>
        /// Gets or sets the Service date.
        /// </summary>
        [JsonProperty("OrderDate")]
        public string OrderDate { get; set; }

        /// <summary>
        /// </summary>
        [JsonProperty("ReqTime")]
        public string ReqTime { get; set; }

        /// <summary>
        /// Gets or sets the reserve.
        /// </summary>
        [JsonProperty("Reserve")]
        public string Reserve { get; set; }

        /// <summary>
        /// </summary>
        [JsonProperty("TransCode")]
        public string TransCode { get; set; }

        /// <summary>
        /// 银联交易号
        /// </summary>
        [JsonProperty("TransId")]
        public string TransId { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 获取订单状态
        /// </summary>
        /// <param name="x">
        /// 状态编号
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetTransState(string x)
        {
            const string Ms = "新订单,成功,失败,支付中";
            string[] strings = Ms.Split(Convert.ToChar(','));
            return strings[Convert.ToInt32(x)];
        }

        /// <summary>
        /// 进行验签
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetMerSign()
        {
            string str = this.ReqTime + this.OrderDate + this.MerOrderId + this.TransId + this.MerId + this.MerTermId
                         + this.Reserve;

            string rsaSign = RsaUtil.RsaSign(str, this.wapYlConfig.Pvr);
            this.MerSign = rsaSign;
            return rsaSign;
        }

        /// <summary>
        /// 开始查询
        /// </summary>
        /// <returns></returns>
        public string Query()
        { 
            var httpHelper = new HttpHelper();
            httpHelper.Data = "jsonString=" + JsonConvert.SerializeObject(this);
            // return httpHelper.Data = "jsonString=" + JsonConvert.SerializeObject(this);
            httpHelper.Url = this.Url;
            return httpHelper.PostPage();
        }

        #endregion
    }
}